package com.frostmourne.mxsguimaker;

import com.frostmourne.mxsguimaker.commands.CommandHandler;
import com.frostmourne.mxsguimaker.commands.sub.ForceOpenCommand;
import com.frostmourne.mxsguimaker.commands.sub.OpenCommand;
import com.frostmourne.mxsguimaker.commands.sub.ReloadCommand;
import com.frostmourne.mxsguimaker.listeners.InventoryListener;
import com.frostmourne.mxsguimaker.listeners.PlayerListener;
import com.frostmourne.mxsguimaker.objects.GuiMakerMold;
import com.frostmourne.mxsguimaker.objects.PAPIHooker;
import com.frostmourne.mxsguimaker.objects.PlayerData;
import com.frostmourne.mxsguimaker.objects.ScriptExpand;
import com.gitee.mxstarsex.objects.PluginLogger;
import com.gitee.mxstarsex.objects.builders.bukkit.listeners.ListenerBuilder;
import com.gitee.mxstarsex.objects.managers.FileManager;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;

import java.io.File;
import java.util.List;

public class MXSGuiMaker extends JavaPlugin {

    public static MXSGuiMaker INSTANCE;
    public static FileManager fileManager;
    public static PluginLogger pluginLogger;

    @Override
    public void onEnable() {

        INSTANCE = this;

        new CommandHandler(this)
                .addSubCommand(new ReloadCommand())
                .addSubCommand(new OpenCommand())
                .addSubCommand(new ForceOpenCommand())
                .register();

        new PAPIHooker().register();

        new ListenerBuilder(this).register(new InventoryListener()).register(new PlayerListener());

        fileManager = new FileManager(this);
        pluginLogger = new PluginLogger(this);

        load();

    }

    public ClassLoader getLoader(){
        return this.getClass().getClassLoader();
    }

    public void load(){

        GuiMakerMold.GUI_MAKER_MOLD_MAP.clear();
        ScriptExpand.SCRIPTS.clear();

        saveDefaultConfig();
        reloadConfig();

        saveDefaultResource();

        List<File> files = fileManager.getFiles(getDataFolder() + "/gui");
        if (files != null && files.size() != 0){
            for (File f : files){
                GuiMakerMold guiMakerMold = new GuiMakerMold(f);
                GuiMakerMold.GUI_MAKER_MOLD_MAP.put(guiMakerMold.getName(),guiMakerMold);
            }
        }
        files = fileManager.getFiles(getDataFolder() + "/expand");
        if (files != null && files.size() != 0){
            for (File f : files){
                FileConfiguration config = YamlConfiguration.loadConfiguration(f);
                for (String s : config.getKeys(false)){
                    ScriptExpand.SCRIPTS.put(s,config.getString(s));
                }
            }
        }

        pluginLogger.sendLog(" &f载入内容:");
        pluginLogger.sendLog("     &fGUI界面 " + GuiMakerMold.GUI_MAKER_MOLD_MAP.size() + " 个");
        pluginLogger.sendLog("     &f拓展脚本 " + ScriptExpand.SCRIPTS.size() + " 个");

    }

    public void saveDefaultResource(){

        File defaultExpand = new File(getDataFolder() + "/expand");
        if (defaultExpand.mkdirs()){
            saveResource("expand/example-expand.yml",false);
        }

        File defaultGui = new File(getDataFolder() + "/gui");
        if (defaultGui.mkdirs()){
            saveResource("gui/example.yml",false);
        }

    }


    @Override
    public void onDisable() {

        for (PlayerData data : PlayerData.DATA_MAP.values()){
            data.save();
        }

    }
}
